<?php
//$_GET['action']={add|edit|delete|link|unlink|show|links}
//$_GET['do']={1|0}
//$_GET['ajax']={1|0}
//$_GET['indent']

	include 'include/function.php';
	if($_GET['action']!='show' && !isset($_user_id))go_out(_d('err_need_login'));

function go_out($s) {
	if($_GET['ajax']=='1') {
		echo $s;
		die();
	} else {
		redirect_error($s);
	}
}

function getLinkTitle($id1,$id2,$type) {
	$list=array();
	
	$ret=oj_query("select title from `group` where id=$id1 ");
	$row=mysql_fetch_row($ret);
	if(!isset($row[0])) go_out("group-$id1 not exist");
	$list[0]=htmlspecialchars($row[0]);
	
	if($type=='group') {
		$ret=oj_query("select title from `group` where id=$id2 ");
	} else if($type=='node') {
		$ret=oj_query("select title from node where id=$id2 ");
	} else go_out("bad request: component type");
	$row=mysql_fetch_row($ret);
	if(!isset($row[0])) go_out("$type-$id2 not exist");
	$list[1]=htmlspecialchars($row[0]);
	
	return $list;
}

function existLink($id1,$id2,$type) {
	$query="select create_time from link where container=$id1 and component=$id2 and type='$type' ";
	$ret=oj_query($query);
	$row=mysql_fetch_row($ret);
	return isset($row[0]);
}
function insertLink($id1,$id2,$type) {
	if(existLink($id1,$id2,$type))return;
	$query="insert into link (container, component, type, create_time) values ($id1, $id2, '$type', now()) ";
	oj_query($query);
	oj_query("update `group` set count=count+1 where id=$id1 ");
	if($type=='group')
		oj_query("update `group` set parent_count=parent_count+1 where id=$id2 ");
}
function deleteLink($id1,$id2,$type) {
	if(!existLink($id1,$id2,$type))return;
	$query="delete from link where container=$id1 and component=$id2 and type='$type' ";
	oj_query($query);
	oj_query("update `group` set count=count-1 where id=$id1 ");
	if($type=='group')
		oj_query("update `group` set parent_count=parent_count-1 where id=$id2 ");
}

//----------------------------
	
	$group_ajax=$_GET['ajax'];
	if($group_ajax!='1')$group_ajax=0;
	
	if($_GET['do']=='1') {
		$group_redirect='group.php?action=show';
		if($_GET['action']=='add') {
			$group_title	= get_to_mysql($_POST['title']);
			$group_desc		= get_to_mysql($_POST['description']);
			$query='insert into `group` (title, description, count, parent_count, create_time, modify_time) '
				. "values('$group_title', '$group_desc', 0, 0, now(), now()) ";
		} else if($_GET['action']=='edit') {
			include_once 'include/revision.php';
			$group_title	= get_to_mysql($_POST['title']);
			$group_desc		= get_to_mysql($_POST['description']);
			$group_gid		= get_to_mysql($_POST['id']);
			if(empty($group_gid))go_out('bad request');
			$row=mysql_fetch_row(oj_query("select description from `group` where id=$group_gid "));
			if($row[0]!=stripslashes($_POST['description']))
				insert_revision($group_gid,'group',$_user_id,'',$row[0],stripslashes($_POST['description']));
			$query="update `group` set title='$group_title', description='$group_desc', modify_time=now() where id='$group_gid' ";
			$group_redirect="group.php?action=show&id=$group_gid";
		} else if($_GET['action']=='delete') {
			$group_gid = get_to_mysql($_POST['id']);
			$query="delete from `group` where id=$group_gid ";
			//TODO
		} else if($_GET['action']=='link'||$_GET['action']=='unlink') {
		//link/unlink
			$group_id1	= get_to_mysql($_POST['id1']);
			$group_id2	= get_to_mysql($_POST['id2']);
			$group_type	= get_to_mysql($_POST['type']);
			if(empty($group_id1)||empty($group_id2)||empty($group_type))go_out('bad request');
			if($group_type!='group' && $group_type!='node') go_out('bad request: component type');
			if($group_id1==$group_id2 && $group_type=='group' && $_GET['action']=='link') go_out('bad request');
			if($_GET['action']=='link') {
				insertLink($group_id1,$group_id2,$group_type);
			} else if($_GET['action']=='unlink') {
				deleteLink($group_id1,$group_id2,$group_type);
			} else go_out('bad request');
			if($group_type=='node')
				$group_redirect="shownode.php?id=$group_id2";
			else if($_GET['action']=='unlink')
				$group_redirect="group.php?action=show&id=$group_id1";
		} else if($_GET['action']=='links') {
		//links
			$group_id2	= get_to_mysql($_POST['id2']);
			$group_type	= get_to_mysql($_POST['type']);
			if(empty($group_id2)||empty($group_type))go_out('bad request');
			if(isset($_POST['links'])){
				foreach($_POST['links'] as $key=>$val){
					insertLink($key,$group_id2,$group_type);
				}
			}
			if(isset($_POST['unlinks0'])){
				foreach($_POST['unlinks0'] as $key=>$val){
					if(!isset($_POST['unlinks'][$key]))
						deleteLink($key,$group_id2,$group_type);
				}
			}
			if($group_type=='node')$group_redirect="shownode.php?id=$group_id2";
			else $group_redirect="group.php?action=show&id=$group_id2";
		} else go_out('bad request');
		if(isset($query))$ret=oj_query($query);
		if($group_ajax) {
			echo 'done';
			die();
		} else {
			redirect($group_redirect);
		}
	}
	
	if(!$group_ajax) {
		$page_title=$site_title . ' - ' . _d('title_group');
		include 'include/header.php';
	}
	
	include_once 'include/grouplist.php';
	if($_GET['action']=='show') {
		if($_GET['id']>0) {
			$group_id=get_to_mysql($_GET['id']);
			$ret=oj_query("select title, description from `group` where id=$group_id ");
			$row=mysql_fetch_row($ret);
			if(!isset($row[0])) go_out("group-$group_id not exist");
			echo '<div class="problem_content">';
				echo '<center>';
					echo "<div class='subtitle'>" . htmlspecialchars($row[0]) . "</div>";
					echo "[<a href='group.php?action=edit&id=$group_id'>" . _d('g_edit') . '</a>]&nbsp;';
					echo "[<a href='history.php?id=$group_id&type=group'>" . _d('g_revision') . '</a>]&nbsp;';
				echo '</center>';
				echo '<div id="main_content">' . expand_include(0,$row[1]) . '</div>';
			if(substr($_user_theme,0,3)=='tb_') { //老式的三个列表依次列出的形式
				echo '</div>'; //content
				//echo 'Nodes:';
				draw_group_list(GL_GROUP_NODE_LIST,0,$group_id);
				echo _d('g_parent_list');
				draw_group_list(GL_GROUP_PARENT_LIST,0,$group_id);
				echo _d('g_child_list');
				draw_group_list(GL_GROUP_CHILD_LIST,0,$group_id);
			} else {
				//父-本-子组
				//      子树
				echo '<div style="height:32px;"></div>';
				echo '<div style="float:left;width:44%"><div class="list_wapper">';
					echo _d('g_parent_list');
					draw_group_list(GL_GROUP_PARENT_LIST,0,$group_id);
				echo '</div></div>';
				echo '<div class="line_box" style="width:2%"><div class="line"></div></div>';
				echo '<div style="float:left;width:8%"><div class="list_wapper">';
					echo str_to_html($row[0]);
				echo '</div></div>';
				echo '<div class="line_box" style="width:2%"><div class="line"></div></div>';
				echo '<div style="float:left;width:44%"><div class="list_wapper">';
					echo _d('g_child_list');
					draw_group_list(GL_GROUP_CHILD_LIST,0,$group_id);
					echo _d('g_node_list');
					draw_group_list(GL_GROUP_NODE_LIST,0,$group_id);
				echo '</div></div>';
				echo '<div style="clear:both"></div>';
				echo '</div>'; //content
			}
		} else draw_group_list(GL_GROUP_HOMEPAGE,0,0);
	} else if($_GET['action']=='add') {
		include_once 'include/form.php';
		$group_check_title='onChange=\'checkTitle(this,$("group_check_title_box"),"group");\'';
		$group_form=array(
			'title'=>array('input',_d('a_title'),'','maxlength="200" ' . $group_check_title),
			'check_title'=>array('html','','<div id="group_check_title_box"></div>'),
			'description'=>array('textarea',_d('a_description'),'',' class="param_use_include" ')
		);
		if($group_ajax) {
			$group_form['submit']=array('html','',"<input type='button' value='Submit'>");
		} else {
			$group_form['submit']=array('submit','','Submit');
		}
		draw_simple_form('post',"group.php?action=add&do=1&ajax=$group_ajax",array(),$group_form);
		echo '<script type="text/javascript" src="include/textarea.js"></script>';
	} else if($_GET['action']=='edit') {
		include_once 'include/form.php';
		$group_id	= get_to_mysql($_GET['id']);
		$row=mysql_fetch_assoc(oj_query("select * from `group` where id='$group_id' limit 1"));
		if(!isset($row['title']))go_out("bad request: group-$group_id not found");
		$group_check_title='onChange=\'checkTitle(this,$("group_check_title_box"),"group");\'';
		$group_form=array(
			'id'=>array('html',_d('a_gid'),"<a href='group.php?action=show&id=$group_id'>$group_id</a>"),
			'title'=>array('input',_d('a_title'),$row['title'],'maxlength="200" ' . $group_check_title),
			'check_title'=>array('html','','<div id="group_check_title_box"></div>'),
			'description'=>array('textarea',_d('a_description'),$row['description'],' class="param_use_include" ')
		);
		if($group_ajax) {
			$group_form['submit']=array('html','',"<input type='button' value='Submit'>");
		} else {
			$group_form['submit']=array('submit','','Submit');
		}
		draw_simple_form('post',"group.php?action=edit&do=1&ajax=$group_ajax",array('id'=>$group_id),$group_form);
		echo '<script type="text/javascript" src="include/textarea.js"></script>';
	} else if($_GET['action']=='delete') {
	} else if($_GET['action']=='link' || $_GET['action']=='unlink') {
		$group_id1	= get_to_html($_GET['id1']);
		$group_id2	= get_to_html($_GET['id2']);
		$group_type	= get_to_html($_GET['type']);
		if(strlen($group_id1)&&strlen($group_id2)&&strlen($group_type)) {
			//link/unlink check/confirm
			if($_GET['action']=='unlink' && (!existLink($group_id1,$group_id2,$group_type)))
				go_out('bad request: not linked');
			$group_title=getLinkTitle($group_id1,$group_id2,$group_type);
			//show
			echo '<center style="font-size:2em;padding-bottom:1em;">';
				printf(_d("cf_{$_GET['action']}_{$group_type}_to_group"),$group_id2,$group_title[1],$group_id1,$group_title[0]);
			echo '</center>';
			include_once 'include/form.php';
			draw_simple_form('post',"group.php?action={$_GET['action']}&do=1&ajax=$group_ajax",
				array(
					'id1'=>$group_id1,
					'id2'=>$group_id2,
					'type'=>$group_type),
				array(
					'submit'=>array('submit','',_d('cf_yes'))
				));
		} else if($_GET['action']=='link'&&strlen($group_id2)&&strlen($group_type)) {
			//link select parent
			if(!$group_ajax) {
				echo '<center style="font-size:2em;padding-bottom:1em;">';
					if($group_type=='group'||$group_type=='node') {
						$ret=oj_query("select title from `$group_type` where id=$group_id2 ");
						$row=mysql_fetch_row($ret);
						printf(_d("cf_link_{$group_type}_to"),$group_id2,htmlspecialchars($row[0]));
					}
				echo '</center>';
			}
			if($group_type=='node')
				draw_group_list(GL_NODE_LINK_GROUP,0,$group_id2);
			else
				draw_group_list(GL_GROUP_LINK_GROUP,0,$group_id2);
		} else echo "<center>bad request:$group_id1,$group_id2,$group_type</center>";
	}/* else if($_GET['action']=='unlink') {
		$group_id1	= get_to_html($_GET['id1']);
		$group_id2	= get_to_html($_GET['id2']);
		$group_type	= get_to_html($_GET['type']);
		if(strlen($group_id1)&&strlen($group_id2)&&strlen($group_type)) {
			//unlink check/confirm
			if(!exist_row('link',"container=$group_id1 and component=$group_id2 and type='$group_type'"))
				go_out('bad request: not linked');
			$group_title=getLinkTitle($group_id1,$group_id2,$group_type);
			//show
			echo '<center style="font-size:2em;padding-bottom:1em;">';
				printf(_d("cf_unlink_{$group_type}_to_group"),$group_id2,$group_title[1],$group_id1,$group_title[0]);
			echo '</center>';
			include_once 'include/form.php';
			draw_simple_form('post',"group.php?action=unlink&do=1&ajax=$group_ajax",
				array(
					'id1'=>$group_id1,
					'id2'=>$group_id2,
					'type'=>$group_type),
				array(
					'submit'=>array('submit','',_d('cf_yes'))
				));
		}
	}*/

	if(!$group_ajax) {
		include 'include/footer.php';
	}
?>

